package TUILayer;
import java.util.*;
import ControlLayer.DVDCtr;
/**
 * User interface to the DVD controller
 * @author(kbh)
 * @version(2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 * (KBH): 2008.10.24: dvdmenu changed to If                   
 */

public class DvdUI
{
    private DVDCtr dvdCtr;//DVD UI must know the DVD controller 
    
    
    public DvdUI()
    {
        
    }
    
    public void start()
    {
        dvdCtr= new DVDCtr();
        dvdMenu();
    }
    
    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choise =   writeDVDMenu();
            if (choise == 1)
            {
                findDVD();
            }
            else{if(choise == 2){
                   createDVD();
                }
                else{
                    if(choise == 3){
                        createCopy();
                    }
                    else{
                        if(choise == 4){
                             deleteDVD();
                        }
                        else{
                            if(choise == 5){
                                listAllDvds();
                            }
                            else{
                                exit = true;
                            }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
        }//end while
    }
    private int writeDVDMenu()
    {
           // creates an object keyboard to read data from the keyboard
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** DVD Menu ***");
            System.out.println(" (1) Show DVD information");
            System.out.println(" (2) Create DVD");
            System.out.println(" (3) Create DVD sample");
            System.out.println(" (4) Delete DVD");
            System.out.println(" (5) Show current DVD list");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choise: ");
            
            int choise = keyboard.nextInt();
            return choise;
    }
    private int inputDVDNo()
    {
        // creates an object keyboard to read data from the keyboard
        boolean ok = false;
        int no = 0;
        Scanner keyboard = new Scanner(System.in);
        while(!ok)
        {
            System.out.println("Type DVD number: ");
            try
            {
                no = keyboard.nextInt();
                ok = true;
            }
           catch (Exception e)
            {
             System.out.println("Input must be a number - try again.");
             String input = keyboard.nextLine();
           }
        }
        return no;
    }
        
    private String inputTitle()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD title: " );
        String title = keyboard.nextLine();
        return title;
    }
    private int inputCopyNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the copy number: ");
        int no = keyboard.nextInt();
        return no;
    }
    private String inputArtist()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the artist name: ");
        String artist = keyboard.nextLine();
        return artist;
    }
    private String inputReleaseDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the release date: ");
        String rDate = keyboard.nextLine();
        return rDate;
    }
    private int inputSerialNr()
    {
        // creates an object keyboard to read data from the keyboard
        boolean ok = false;
        int no = 0;
        Scanner keyboard = new Scanner(System.in);
        while(!ok)
        {
            System.out.println("Type serial number: ");
            try
            {
                no = keyboard.nextInt();
                ok = true;
            }
           catch (Exception e)
            {
             System.out.println("Input must be a number - try again.");
             String input = keyboard.nextLine();
           }
        }
        return no;
    }
    private String inputAcqDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the acquisition date: ");
        String acqDate = keyboard.nextLine();
        return acqDate;
    }
    private double inputAcqPrice()
    {
        // creates an object keyboard to read data from the keyboard
        boolean ok = false;
        double no = 0;
        Scanner keyboard = new Scanner(System.in);
        while(!ok)
        {
            System.out.println("Type acquisition price: ");
            try
            {
                no = keyboard.nextDouble();
                ok = true;
            }
           catch (Exception e)
            {
             System.out.println("Input must be a number - try again.");
             String input = keyboard.nextLine();
           }
        }
        return no;
    }
    
    //there is need for more methods to read information about DVDs
    //and methods to display the information
  
    private void findDVD()
    {     
        int no = inputDVDNo();
        //is to be changed when the DVD controller is completed
        if(dvdCtr.findDVD(no) != null)
        {
            System.out.println(dvdCtr.findDVD(no).getArtist());
            System.out.println(dvdCtr.findDVD(no).getId());
            System.out.println(dvdCtr.findDVD(no).getReleaseDate());
            System.out.println(dvdCtr.findDVD(no).getTitle());
            pause();
        }
        else
        {
            System.out.println("There are no DVD's with that id number.");
            pause();
        }
    }
    private void createDVD()
    {
        int id = inputDVDNo();
        if(dvdCtr.findDVD(id) != null)
        {
            if(id != dvdCtr.findDVD(id).getId())
            {
                dvdCtr.createDVD(id, inputTitle(), inputArtist(), inputReleaseDate());
                pause();
            }
            else
            {
                System.out.println("Id already exist.");
                pause();
            }
        }
        else
        {
            dvdCtr.createDVD(id, inputTitle(), inputArtist(), inputReleaseDate());
            pause();
        }
    }
    private void createCopy()
    {
        int id = inputDVDNo();
        if(dvdCtr.findDVD(id) != null)
        {
            if(id == dvdCtr.findDVD(id).getId())
            {
                dvdCtr.createSample(id, inputSerialNr(), inputAcqDate(), inputAcqPrice());
                System.out.println("Done");
                pause();
                
            }
            else
            {
                System.out.println("ID does not exist.");
                pause();
            }
        }
        else
        {
            System.out.println("ID does not exist");
            pause();
        }
        
    }
    
    private void deleteDVD()
    {
        int id = inputDVDNo();
        System.out.println(dvdCtr.findDVD(id).getTitle() + "Will be deleted.");
        dvdCtr.deleteDVD(id);
        pause();
    }
    private void listAllDvds()
    {
        ArrayList<String> list;
        list = dvdCtr.listAllDvds();
        if(!list.isEmpty())
        {
            for(String d : list)
            {
                System.out.println(d);
            }
            list.size();
            pause();
            dvdCtr.listAllDvds().clear();
        }
        else
        {
            System.out.println("There are no DVD's in the system.");
            pause();
        }
    }
    private void pause()
    {
       Scanner keyboard = new Scanner(System.in);
       String wait = keyboard.nextLine();
    }
    public void findDVDforRent(int _id)
    {     
        int no = _id;
        //is to be changed when the DVD controller is completed
        if(dvdCtr.findDVD(no) != null)
        {
            System.out.println(dvdCtr.findDVD(no).getArtist());
            System.out.println(dvdCtr.findDVD(no).getTitle());
            pause();
        }
        else
        {
            System.out.println("There are no DVD with that id number.");
            pause();
        }
    }
    public int getDVDId(int _id)
    {
        return dvdCtr.getDVDID(_id);
    }
}